package time_task

import (
	. "chess_platform/internal/common"
	"fmt"
	"time"
)

//玩家方面的数据统计,如日活，日注册,留存率等

func StatPlayerInfo() error {
	var (
		t              = time.Now()
		lastMonth      = GetSpecifyDayByTimestamp(t.Unix(), -30)
		start, end     = StartEndTimeByTimestamp(t.Unix())                   //
		spStart, spEnd = StartEndTimeByTimestamp(t.AddDate(0, 0, -1).Unix()) //次日留存率的时间
		dau            int64
		mau            int64 //月活
		userNum        int64 //每天在玩的玩家数
		dayRegister    int64
		totalRegister  int64
		retentionRate  float64 //留存率
	)

	//日活
	sqlDau := fmt.Sprintf(`
		SELECT
			COUNT(1)
		FROM
			%v
		WHERE
			logintime >=%v AND logintime <= %v
	`, TableClientUser, start, end)
	//月活
	sqlMau := fmt.Sprintf(`
		SELECT
			COUNT(1)
		FROM
			%v
		WHERE
			logintime >=%v AND logintime <= %v
	`, TableClientUser, lastMonth, t.Unix())
	//day_register,日注册
	sqlDayRegister := fmt.Sprintf(`
		SELECT
			COUNT(1)
		FROM
			%v
		WHERE
			rtime >=%v AND rtime <= %v
	`, TableClientUser, start, end)
	//total_register,累计注册
	sqlTotalRegister := fmt.Sprintf(`
		SELECT
			COUNT(1)
		FROM
			%v
	`, TableClientUser)
	//留存率
	sqlRetentionRate := fmt.Sprintf(`
		SELECT
			((SELECT COUNT(1) FROM %v WHERE logintime >= %v AND logintime <= %v
			AND rtime >= ? AND rtime <= ?) /
			(SELECT COUNT(1) FROM %v WHERE rtime >= ? AND rtime <= ?))
	`, TableClientUser, start, end, TableClientUser)
	//每天游戏人数
	sqlUser := fmt.Sprintf(`
		SELECT
			COUNT(DISTINCT r.uid) AS user_num
		FROM
			%v AS p
		LEFT JOIN
			%v AS r
		ON p.tableid = r.tableid
		WHERE
			p.endtime >= %v AND p.endtime <= %v
	`, TableRoom, TableRoomLog, start, end)

	o := NewDBOrm(DBChess)
	err := o.Raw(sqlDau).QueryRow(&dau)
	if err != nil {
		LogPc.Error(err.Error())
	}
	err = o.Raw(sqlMau).QueryRow(&mau)
	if err != nil {
		LogPc.Error(err.Error())
	}

	err = o.Raw(sqlUser).QueryRow(&userNum)
	if err != nil {
		LogPc.Error(err.Error())
	}

	err = o.Raw(sqlDayRegister).QueryRow(&dayRegister)
	if err != nil {
		LogPc.Error(err.Error())
	}
	err = o.Raw(sqlTotalRegister).QueryRow(&totalRegister)
	if err != nil {
		LogPc.Error(err.Error())
	}
	err = o.Raw(sqlRetentionRate, spStart, spEnd, spStart, spEnd).QueryRow(&retentionRate)
	if err != nil {
		LogPc.Error(err.Error())
	}

	data := map[string]interface{}{
		"create_time":    end - 1, //当天的23:59:59
		"dau":            dau,
		"mau":            mau,
		"user_num":       userNum,
		"day_register":   dayRegister,
		"total_register": totalRegister,
		"two_day":        retentionRate * 10000,
	}
	spStart, spEnd = StartEndTimeByTimestamp(t.AddDate(0, 0, -2).Unix()) //3日留存率的时间
	err = o.Raw(sqlRetentionRate, spStart, spEnd, spStart, spEnd).QueryRow(&retentionRate)
	if err != nil {
		LogPc.Error(err.Error())
	}
	data["three_day"] = retentionRate * 10000

	spStart, spEnd = StartEndTimeByTimestamp(t.AddDate(0, 0, -6).Unix()) //7日留存率的时间
	err = o.Raw(sqlRetentionRate, spStart, spEnd, spStart, spEnd).QueryRow(&retentionRate)
	if err != nil {
		LogPc.Error(err.Error())
	}
	data["seven_day"] = retentionRate * 10000

	_, err = NewOrUpdateChessLog(data, TableDayPlayerInfo)
	return err
}
